﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;

namespace ExcelGenerator.SpreadSheet.Styles.Attributes
{

    /// <summary>
    /// Options available for format property
    /// </summary>
    public enum Format
    {
        General, 
        [Description("General Number")]
        GeneralNumber,
        [Description("General Date")]
        GeneralDate,
        [Description("Long Date")]
        LongDate,
        [Description("Medium Date")]
        MediumDate,
        [Description("Short Date")]
        ShortDate,
        [Description("Long Time")]
        LongTime,
        [Description("Medium Time")]
        MediumTime,
        [Description("Short Time")]
        ShortTime,
        Currency,
        [Description("Euro Currency")]
        EuroCurrency,
        Fixed,
        Standard,
        Percent,
        Scientific,
        [Description("Yes/No")]
        YesNo,
        [Description("True/False")]
        TrueFalse,
        [Description("On/Off")]
        OnOff
    }

    /// <summary>
    /// Defines the number format that should be in cells referencing this style. The default value is General, and all other number formats require a custom format code.
    /// </summary>
    public class NumberFormat
    {
        /// <summary>
        /// Specifies the name of the font. This string is case insensitive. If this attribute is not specified within an element, the default(Arial) is assumed.
        /// </summary>
        public Format format { get; set; }

        public NumberFormat(Format format)
        {
            this.format = format;
        }

        internal String render()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<NumberFormat ss:Format=\"");
            sb.Append(GetEnumDescription(format));
            sb.Append("\" />");

            return sb.ToString();
        }

        private static string GetEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attributes != null && attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }
    }
}
